﻿global using HtmlAgilityPacknetcore;


using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using AngleSharp.Html.Parser;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Intrinsics.X86;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace HtmlAgilityPacknetcore
{
    internal partial class Program
    {
        static void Main(string[] args)
        {
            var generator = new ClassGenerator();
            var ss = Directory.GetFiles("E:\\RM\\HtmlAgilityPacknetcore\\html");
            List<ClassInfo> list = new List<ClassInfo>();
            foreach (var p in ss)
            {
                string htmlContent = File.ReadAllText(p);
                var parser = new HtmlParser();
                // 解析 HTML
                var document = parser.ParseDocument(htmlContent);

                // 使用 CSS 选择器选择元素
                var Mainpage = document.QuerySelector(".doc-content.option-example-right-layout");//页面
                                                                                                  //ClassInfo legend = new ClassInfo() { Name = "legend" };
                                                                                                  //list.Add(legend);
                var na = Path.GetFileNameWithoutExtension(p);
                Getclass(Mainpage, list, na);
                foreach (var item in list)
                {
                    var code = generator.GenerateClass($"SharedPage.Model.{na}", item.Name.Replace("-","_"), item.ClassProps, item.Descri);
                    Directory.CreateDirectory($"echarts\\{na}");
                    string filePath22 = Path.Combine($"echarts\\{na}", $"{item.Name}.cs"); // 新文件的路径

                    File.WriteAllText(filePath22, code);
                }
                list.Clear();
            }

       

            // 创建 AngleSharp 的 HTML 解析器





         



     


        }
        public static void Getclass(IElement Mainpage, List<ClassInfo> list, string name = "weizhi")
        {
            ClassInfo legend = new ClassInfo() { };
            if (Mainpage.Id == null)
            {
                legend.Name = name;
            }
            else
            {
                legend.Name = Mainpage.Id.Substring(Mainpage.Id.LastIndexOf('-') + 1).Replace("-","_"); ;
            }
            list.Add(legend);
            if (Mainpage != null)
            {
                var childDivs = Mainpage.Children;//有一个描述集合和一个属性集合
                foreach (var childDiv in childDivs)
                {

                    if (childDiv.NodeType == NodeType.Element && ((IHtmlElement)childDiv).NodeName.ToLower() == "div")
                    {
                        //有可能是描述, 也有可是需要的
                        foreach (var cs2 in childDiv.Children)
                        {
                            string pname = "";
                            string pdes = "JsVal?";
                            string ptype = "";
                            if (cs2 is IHtmlElement ele)
                            {
                                if (ele.NodeType == NodeType.Element && ele.ClassName != null && ele.ClassName.Contains("doc-content-item-card"))
                                {
                                    if (ele.Id != null)
                                    {
                                        pname = ele.Id.Substring(ele.Id.LastIndexOf('-') + 1);
                                    }
                                    var prop_types = ele.Children.First(node => node.ClassList.Contains("prop-types")).QuerySelectorAll("span");
                                    var prop_description = ele.Children.First(node => node.ClassList.Contains("item-description"));
                                    foreach (var span in prop_types)
                                    {
                                        var t = span.TextContent.Replace(" ", "").Replace("\r\n", "").Replace("\n", "");
                                        if (t == "number")
                                        {
                                            t = "JsType.Num";
                                        }
                                        else if (t == "boolean")
                                        {
                                            t = "JsType.Bool";
                                        }
                                        else if (t == "Object")
                                        {
                                            t = "JsType.Object";
                                            pdes = pname + "?";
                                            Getclass(cs2, list);
                                            Console.WriteLine(ele.Id);
                                        }
                                        else if (t == "Color")
                                        {
                                            t = "JsType.String";
                                        }
                                        else if (t == "Array")
                                        {
                                            t = "JsType.Array";
                                        }
                                        else if (t == "Function")
                                        {
                                            t = "JsType.Function";
                                        }
                                        else if (t == "string")
                                        {
                                            t = "JsType.String";
                                        }
                                        else
                                        {
                                            t = $"xxxxxxxxxxxxxxxx{t}";

                                        }
                                        ptype += $"< see cref = \"{t}\" /> ";
                                    }
                                    var temp = prop_description.TextContent.Replace(" ", "").Replace("\r\n", "").Replace("\n", "");
                                    ptype += $"<para>{temp}</para>";
                                    legend.ClassProps.Add((pdes, pname, ptype));
                                }
                            }

                        }

                    }
                }
            }
        }
    }

}
